﻿using CodeGeneratorBootstrap.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGeneratorBootstrap.Template.Web.Views
{
    public partial class Add : ITemplate
    {
        public MetaData Data { get; set; }
        public List<LookupClassModel> lookups { get; set; }
        public List<PickupListModel> pickuplist { get; set; }

        public string parent_attr_PK_name = "";
        public string parent_attr_Name_name = "";

        public string parent_class_name = "";

        public string Filename
        {
            get { return "Add{ClassName}.cshtml"; }
        }

        public bool IsGenerateOneTime { get { return false; } }
        public bool IsGenerateFolder { get { return true; } }
        public bool IsRequiredOnlyOneMember { get { return true; } }
        public GenerateStyle GenerateStyle { get { return GenerateStyle.Normal; } }

        // Check if current class model is child class ,if so get parent class info. such as parent class name and attribute pk name
        public bool IsChildClass()
        {
            if (Data.Models[0].ParentClass != null && Data.Models[0].ParentClass.Count > 0)
            {
                parent_class_name = Data.Models[0].ParentClass[0].Name;

                foreach (var attr in Data.Models[0].ParentClass[0]._attributes)
                {
                    if (attr.Pk)
                        parent_attr_PK_name = attr.Name;
                }
                return true;
            }
            else
                return false;
        }

        public String GetPKOfLookup(List<LookupModel> list)
        {
            foreach (LookupModel l in list)
            {
                if (l.DestAttribute.Pk)
                    return l.DestAttribute.Name;
            }
            return "";
        }

        public String GetNameOfLookup(List<LookupModel> list)
        {
            foreach (LookupModel l in list)
            {
                if (!l.DestAttribute.Pk)
                    return l.DestAttribute.Name;
            }
            return "";
        }

        //Calculation Field
        public string IsCalculateOperandReturnFnName(string currentAtt)
        {
            ClassModel Clsmodel = Data.Models[0];
            int a = Clsmodel.Id;
            List<AttributeModel> att = Clsmodel._attributes.FindAll(x => x.FieldType.Name.Equals("Calculation Field"));
            List<string> functions = new List<string>();
            if (att != null)
            {
                foreach (AttributeModel item in att)
                {
                    string[] separators = { "+", "-", "*", "/", "^", "(", ")", " ", ".concat", ".contain", "AddDate", "AddMonth", "AddYear", "Minimum", "Maximun", "[", "]", "DateDiff", ",", "MinDate", "MaxDate", "Summary" };
                    string[] words = item.Formula.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    if (words.Contains(currentAtt))
                    {
                        functions.Add(string.Concat("Calculation", Clsmodel.Name, "_", item.Name, "();")); //function name
                    }
                }
                return string.Join(" ", functions);
            }
            return "";
        }

        /// <summary>
        /// Name : Phuwadet Boonpho
        /// Date : 19/04/2014
        /// Description : ตรวจสอบว่ามี Attribute ที่เป็น Image ใน Class หรือไม่ ถ้ามีต้องสามารถ Upload ได้
        /// </summary>
        /// <param name="model"></param>
        /// <returns>String</returns>
        public string IsFormUpload()
        {
            ClassModel Clsmodel = Data.Models[0];
            List<AttributeModel> list = new List<AttributeModel>();

            foreach (AttributeModel c in Clsmodel._attributes)
            {
                if (c.FieldType.Name == "Image")
                {
                    //return ", new { enctype = \"multipart/form-data\" }"; //APPLE 22/05/2014
                    return ", enctype = \"multipart/form-data\" "; //APPLE 22/05/2014
                }
            }

            return "";
        }

		
		/// <summary>
		/// Create by: Nathawat (11-05-2014)
		/// provide method for check Class has GeoLocation attribute
		/// </summary>
		/// <param name="model">Class Model</param>
		/// <returns>bool</returns>
		public bool HasGeoLocationAttribute(ClassModel model)
		{
			foreach (AttributeModel attribute in model._attributes)
			{
				if (attribute.DataType.Name.Equals("GeoLocation"))
				{
					return true;
				}
			}
			return false;
		}

        //[ding Start]
        public List<LayoutSectionModel> getListLayoutSection()
        {
            ClassModel classModel = Data.Models[0];

            List<LayoutSectionModel> result = new List<LayoutSectionModel>();

            result = classModel.LayoutSection.OrderBy(item => item.SeqNo).ToList();
            
            
            if (!classModel.LayoutSection.Any(cls => cls.Id.Equals(null)))
            {
                result.Add(new LayoutSectionModel()
                {
                    Id = null,
                    LayoutColumn = 2,
                    SectionName = "Information",
                    SeqNo = 0,
                    Width = 100,
                    Height = 100,
                    ShowScrollbars = 0,
                    ShowLabel = 1,
                    TabkeyOrder = 1

                });
            }

            return result;
        }

        public string showScrollbars(LayoutSectionModel listLayoutSection)
        {
            string result = "";

            if (listLayoutSection.ShowScrollbars == 1)
            {
                result = "overflow: scroll;";
                if ((listLayoutSection.Width != -1) && (listLayoutSection.Height != -1))
                {
                    result += "width:" + listLayoutSection.Width + "px; height:" + listLayoutSection.Height + "px;";
                }
                else if ((listLayoutSection.Width != -1) && (listLayoutSection.Height == -1))
                {
                    result += "width:" + listLayoutSection.Width + "px;";
                }
                else if ((listLayoutSection.Width == -1) && (listLayoutSection.Height != -1))
                {
                    result += "height:" + listLayoutSection.Height + "px;";
                }
                else
                {
                    result += "width:auto;height:auto; ";
                }
            }
            else
            {
                result = "width:auto;height:auto; ";
            }

            return result;
        }
        //[Ding end]
    }
}
